<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="用户id" prop="uid">
      <el-input v-model="dataForm.uid" placeholder="用户id"></el-input>
    </el-form-item>
    <el-form-item label="1手机号 2邮箱 3用户名 4qq 5微信 6腾讯微博 7新浪微博" prop="identityType">
      <el-input v-model="dataForm.identityType" placeholder="1手机号 2邮箱 3用户名 4qq 5微信 6腾讯微博 7新浪微博"></el-input>
    </el-form-item>
    <el-form-item label="手机号 邮箱 用户名或第三方应用的唯一标识" prop="identifier">
      <el-input v-model="dataForm.identifier" placeholder="手机号 邮箱 用户名或第三方应用的唯一标识"></el-input>
    </el-form-item>
    <el-form-item label="密码凭证(站内的保存密码，站外的不保存或保存token)" prop="certificate">
      <el-input v-model="dataForm.certificate" placeholder="密码凭证(站内的保存密码，站外的不保存或保存token)"></el-input>
    </el-form-item>
    <el-form-item label="绑定时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="绑定时间"></el-input>
    </el-form-item>
    <el-form-item label="更新绑定时间" prop="updateTime">
      <el-input v-model="dataForm.updateTime" placeholder="更新绑定时间"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          uid: '',
          identityType: '',
          identifier: '',
          certificate: '',
          createTime: '',
          updateTime: '',
          deleted: ''
        },
        dataRule: {
          uid: [
            { required: true, message: '用户id不能为空', trigger: 'blur' }
          ],
          identityType: [
            { required: true, message: '1手机号 2邮箱 3用户名 4qq 5微信 6腾讯微博 7新浪微博不能为空', trigger: 'blur' }
          ],
          identifier: [
            { required: true, message: '手机号 邮箱 用户名或第三方应用的唯一标识不能为空', trigger: 'blur' }
          ],
          certificate: [
            { required: true, message: '密码凭证(站内的保存密码，站外的不保存或保存token)不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '绑定时间不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '更新绑定时间不能为空', trigger: 'blur' }
          ],
          deleted: [
            { required: true, message: '逻辑删除（0.删除 1.未删除）不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/childrenwear_product/product/userauth/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.uid = data.userAuth.uid
                this.dataForm.identityType = data.userAuth.identityType
                this.dataForm.identifier = data.userAuth.identifier
                this.dataForm.certificate = data.userAuth.certificate
                this.dataForm.createTime = data.userAuth.createTime
                this.dataForm.updateTime = data.userAuth.updateTime
                this.dataForm.deleted = data.userAuth.deleted
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/childrenwear_product/product/userauth/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'uid': this.dataForm.uid,
                'identityType': this.dataForm.identityType,
                'identifier': this.dataForm.identifier,
                'certificate': this.dataForm.certificate,
                'createTime': this.dataForm.createTime,
                'updateTime': this.dataForm.updateTime,
                'deleted': this.dataForm.deleted
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
